/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.nubotech.gwt.oss.browser.client.ui;

import com.extjs.gxt.ui.client.util.DateWrapper;
import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.nubotech.gwt.oss.browser.client.Main;
import com.nubotech.gwt.oss.client.OnlineStorageService;
import com.nubotech.gwt.oss.client.OnlineStorageServiceFactory;
import com.nubotech.gwt.oss.client.auth.AuthenticationManager;
import com.nubotech.gwt.oss.client.auth.Credential;
import java.util.Date;

/**
 *
 * @author jonnakkerud
 */
public class CreateUrlWidget extends Composite {
    private BrowserView browserView;
    private VerticalPanel main;
    public CreateUrlWidget(BrowserView browserView) {
        this.browserView = browserView;
        initWidget(main = new VerticalPanel());
        initGui();
    }
    
    private void initGui() {
        //main.setSpacing(5);
        
        HorizontalPanel hp = new HorizontalPanel();
        hp.setSpacing(5);
        main.add(hp);
        
        final ListBox lb = new ListBox();
        lb.addItem("One Day");
        lb.addItem("One Week");
        lb.addItem("One Month");
        lb.addItem("Three Months");
        lb.addItem("One Year");
        hp.add(lb);
        
        hp.add(new Button("Submit", new ClickListener() {
            public void onClick(Widget sender) {
                // create the link
                createLinks(createDate(lb.getSelectedIndex()));
                DeferredCommand.addCommand(new Command() {
                    public void execute() {
                        browserView.refresh();
                    }
                });
                
            }
        }));                
    }
    
    private Date createDate(int index) {
        DateWrapper dw = new DateWrapper();
        switch (index) {
            case 0: dw.addDays(1); break;
            case 1: dw.addDays(5); break;
            case 2: dw.addMonths(1); break;
            case 3: dw.addMonths(3); break;
            case 4: dw.addYears(1);
        }
        
        return dw.asDate();
    }
    
    private void createLinks(Date d) {
        // get selected files
        //browserView.fileView.
        Credential c = AuthenticationManager.instance().getLogin();
        if (c != null) {
            OnlineStorageService oss = OnlineStorageServiceFactory.getService(c);
            
            // for each selected File in fileView
            for (FileView.File f : browserView.fileView.getSelectedFiles()) {
                String url = oss.createGetLink(f.bucket, f.key, d);
                // set cookie: note limitation on number
                GWT.log(url, null);
                //Cookies.setCookie(f.bucket + ":" + f.key, URL.encodeComponent(url), d, null, "/", false);
                Main.put(f.bucket + ":" + f.key, url);
            }
        }        
    }
    
}
